home *** CD-ROM | disk | FTP | other *** search
/ Delphi 2 - Developers' Solutions / Delphi 2 Developers' Solutions.iso / dds / chap02 / howto06 / delphi10 / ufmgr6.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1995-10-13  |  17.2 KB  |  472 lines

  1. unit Ufmgr6;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, StdCtrls, Buttons, ExtCtrls, ShellAPI, FileCtrl,
  8.    DRWSUtl1, DRWSUtl2;
  9.  
  10. type
  11.   TCCFileMgrForm = class(TForm)
  12.     Panel1: TPanel;
  13.     Panel2: TPanel;
  14.     BitBtn1: TBitBtn;
  15.     Panel3: TPanel;
  16.     Label1: TLabel;
  17.     BitBtn2: TBitBtn;
  18.     BitBtn3: TBitBtn;
  19.     BitBtn4: TBitBtn;
  20.     BitBtn5: TBitBtn;
  21.     BitBtn6: TBitBtn;
  22.     BitBtn7: TBitBtn;
  23.     OpenDialog1: TOpenDialog;
  24.     BitBtn8: TBitBtn;
  25.     BitBtn9: TBitBtn;
  26.     OpenDialog2: TOpenDialog;
  27.     procedure FormResize(Sender: TObject);
  28.     procedure FormCreate(Sender: TObject);
  29.     procedure BitBtn7Click(Sender: TObject);
  30.     procedure BitBtn1Click(Sender: TObject);
  31.     procedure BitBtn2Click(Sender: TObject);
  32.     procedure BitBtn3Click(Sender: TObject);
  33.     procedure BitBtn4Click(Sender: TObject);
  34.     procedure BitBtn5Click(Sender: TObject);
  35.     procedure DirectoryListBox1Change(Sender: TObject);
  36.     procedure FormPaint(Sender: TObject);
  37.     procedure FileListBox1DblClick(Sender: TObject);
  38.     procedure BitBtn8Click(Sender: TObject);
  39.     procedure BitBtn9Click(Sender: TObject);
  40.   private
  41.     { Private declarations }
  42.   public
  43.     { Public declarations }
  44.     DirectoryListBox1 : TDirectoryListBox;
  45.     FileListBox1 : TIconFileListBox;
  46.     ScrollBox1 : TFileIconPanelScrollbox;
  47.   end;
  48. var
  49.   CCFileMgrForm      : TCCFileMgrForm;
  50.   CurrentView        : Integer;  { This holds the current view setting      }
  51.  
  52. implementation
  53.  
  54. {$R *.DFM}
  55.  
  56. uses DDDFUnit;  { Include custom directory selection form }
  57.  
  58. { Set up the resize to replace the icon buttons }
  59. procedure TCCFileMgrForm.FormResize(Sender: TObject);
  60. begin
  61.   { Send in the panel and global buttons list }
  62.   SpacePanelButtons( Panel2 );
  63. end;
  64.  
  65. { Delphi wrote this; use it to set everything up }
  66. procedure TCCFileMgrForm.FormCreate(Sender: TObject);
  67. var TheIconPanel : TFileIconPanel;
  68.     CurrentPath : String;
  69. begin
  70.   { Create the directory list box }
  71.   DirectoryListBox1 := TDirectoryListBox.Create( Panel1 );
  72.   DirectoryListBox1.Parent := Panel1;
  73.   DirectoryListBox1.Visible := true;
  74.   DirectoryListbox1.Top := 17;
  75.   DirectoryListbox1.Left := 0;
  76.   DirectoryListbox1.Height := Panel1.Height - 32;
  77.   DirectoryListbox1.Width := 0;
  78.   { Create the file list box }
  79.   FileListBox1 := TIconFileListBox.Create( Panel1 );
  80.   FileListBox1.Parent := Panel1;
  81.   FileListbox1.Top := 17;
  82.   FileListbox1.Left := 146;
  83.   FileListbox1.Height := Panel1.Height - 32;
  84.   FileListbox1.Width := 0;
  85.   FileListBox1.Visible := true;
  86.   FileListBox1.ShowGlyphs := true;
  87.   { Set intercomponent interactions }
  88.   DirectoryListBox1.FileList := FileListbox1;
  89.   DirectoryListBox1.DirLabel := Label1;
  90.   { Create the scrollbox }
  91.   ScrollBox1 := TFileIconPanelScrollBox.Create( Panel1 );
  92.   ScrollBox1.Parent := Panel1;
  93.   ScrollBox1.align := alClient;
  94.   ScrollBox1.Left := 0;
  95.   ScrollBox1.Top := 0;
  96.   ScrollBox1.Width := Panel1.Width - 32;
  97.   ScrollBox1.Height := Panel1.Height - 32;
  98.   ScrollBox1.IconsNeedRefreshing := false;
  99.   ScrollBox1.TheStoredHandle := Self.Handle;
  100.   { Get the current directory with 0 parameter }
  101.   GetDir( 0 , CurrentPath );
  102.   { Set the display caption }
  103.   Label1.Caption := CurrentPath;
  104.   { Add a trailing \ if not root }
  105.   CurrentPath := ScrollBox1.TheFWB.ForceTrailingBackSlash( CurrentPath );
  106.   { Get the icons display using scrollbox1 }
  107.   ScrollBox1.GetIconsForEntireDirectory( CurrentPath );
  108.   { Set the Icon View as default }
  109.   CurrentView := 1;
  110. end;
  111.  
  112. { Delphi wrote this; use it to close the application }
  113. procedure TCCFileMgrForm.BitBtn7Click(Sender: TObject);
  114. begin
  115.   Close;
  116. end;
  117.  
  118. { Delphi wrote this use it to perform a copy on all selected files }
  119. procedure TCCFileMgrForm.BitBtn1Click(Sender: TObject);
  120. var ThePosition  : Integer;       { Loop counter   }
  121.     CurrentName ,                 { Holds work name}
  122.     TheOldString : String;        { Holds Dir      }
  123.     finished     : Boolean;       { Loop control   }
  124.     WorkingDir   : String;        { Holds mod wd   }
  125.     TargetDir    : String;        { target of op   }
  126.     TheResult    : Integer;       { Modal res hold }
  127. begin
  128.   { Get current directory and save it for later }
  129.   GetDir( 0 , TheOldString );
  130.   { Check for need to add trailing \ }
  131.   WorkingDir := TheOldString;
  132.   WorkingDir := ScrollBox1.TheFWB.ForceTrailingBackSlash( WorkingDir );
  133.   { Display the Windows-based directory input form }
  134.   TheResult := DestDDForm.ShowModal;
  135.   { If not cancelled do the copy }
  136.   if TheResult = mrOK then
  137.   begin
  138.     { Get the target directory with \ OK }
  139.     TargetDir := DestDDForm.GetTargetDirectory;
  140.     { Show the hourglass to indicate waiting }
  141.     Screen.Cursor := crHourGlass;
  142.     { Setup to get all selections }
  143.     ThePosition := 1;
  144.     finished := false;
  145.     while not finished do
  146.     begin
  147.       { Call generic file getting routine based on current view}
  148.       case CurrentView of
  149.         1 : CurrentName := ScrollBox1.GetNextSelection( WorkingDir ,
  150.              ThePosition );
  151.         2 : CurrentName := FileListBox1.GetNextSelection( WorkingDir ,
  152.              ThePosition );
  153.       end;
  154.       { If returns blank string out of selections so exit }
  155.       if CurrentName = '' then finished := true else
  156.       begin
  157.         { If a directory signal error }
  158.         if (( FileGetAttr( CurrentName ) and faDirectory ) = faDirectory ) then
  159.         begin
  160.           if MessageDlg( 'Copy Directory ' + CurrentName + ' to ' + TargetDir
  161.            +'?', mtConfirmation , mbYesNoCancel , 0 ) = mryes then
  162.           begin
  163.             ScrollBox1.TheFWB.RecursivelyCopyDirectory( CurrentName ,
  164.              TargetDir );
  165.           end;
  166.         end
  167.         else
  168.         begin
  169.           Scrollbox1.TheFWB.CopyTheFile( CurrentName , TargetDir );
  170.         end;
  171.       end;
  172.     end;
  173.     { Reset to normal cursor }
  174.     Screen.Cursor := crDefault;
  175.   end;
  176.   { Reset to the original directory }
  177.   ChDir( TheOldString );
  178. end;
  179.  
  180. { Delphi wrote this, use it to move files which are selected }
  181. procedure TCCFileMgrForm.BitBtn2Click(Sender: TObject);
  182. var ThePosition  : Integer;       { Loop counter   }
  183.     CurrentName ,                 { Holds work name}
  184.     TheOldString : String;        { Holds Dir      }
  185.     finished     : Boolean;       { Loop control   }
  186.     WorkingDir   : String;        { Holds mod wd   }
  187.     TargetDir    : String;        { target of op   }
  188.     TheResult    : Integer;       { Modal res hold }
  189. begin
  190.   { Get current directory and save it for later }
  191.   GetDir( 0 , TheOldString );
  192.   { Check for need to add trailing \ }
  193.   WorkingDir := TheOldString;
  194.   WorkingDir := ScrollBox1.TheFWB.ForceTrailingBackSlash( WorkingDIr );
  195.   { Display the Windows-based directory input form }
  196.   TheResult := DestDDForm.ShowModal;
  197.   { If not cancelled do the copy }
  198.   if TheResult = mrOK then
  199.   begin
  200.     { Get the target directory with \ OK }
  201.     TargetDir := DestDDForm.GetTargetDirectory;
  202.     { Show the hourglass to indicate waiting }
  203.     Screen.Cursor := crHourGlass;
  204.     { Set up to get all current selections }
  205.     ThePosition := 1;
  206.     finished := false;
  207.     while not finished do
  208.     begin
  209.       { Call generic file getting routine based on current view}
  210.       case CurrentView of
  211.         1 : CurrentName := ScrollBox1.GetNextSelection( WorkingDir ,
  212.              ThePosition );
  213.         2 : CurrentName := FileListBox1.GetNextSelection( WorkingDir ,
  214.              ThePosition );
  215.       end;
  216.       { If returns blank string then out of selections }
  217.       if CurrentName = '' then finished := true else
  218.       begin
  219.         { If a directory signal error }
  220.         if (( FileGetAttr( CurrentName ) and faDirectory ) = faDirectory ) then
  221.         begin
  222.           if MessageDlg( 'Move Directory ' + CurrentName + ' to ' + TargetDir +
  223.            '?' , mtConfirmation , mbYesNoCancel , 0 ) = mrYes then
  224.             ScrollBox1.TheFWB.RecursivelyMoveDirectory( CurrentName ,
  225.              TargetDir );
  226.         end
  227.         else
  228.         begin
  229.           Scrollbox1.TheFWB.MoveTheFile( CurrentName , TargetDir );
  230.         end;
  231.       end;
  232.       { Reset to normal cursor }
  233.       Screen.Cursor := crDefault;
  234.     end;
  235.   end;
  236.   { Reset to the original directory }
  237.   ChDir( TheOldString );
  238.   if CurrentView = 1 then { Reset icon display if in view }
  239.    ScrollBox1.Update else FileListBox1.Update; { Otherwise update the FLB }
  240. end;
  241.  
  242. { Delphi wrote this, use it to delete files which are selected }
  243. procedure TCCFileMgrForm.BitBtn3Click(Sender: TObject);
  244. var ThePosition  : Integer;       { Loop counter   }
  245.     CurrentName ,                 { Holds work name}
  246.     TheOldString : String;        { Holds Dir      }
  247.     finished     : Boolean;       { Loop control   }
  248.     WorkingDir   : String;        { Holds mod wd   }
  249. begin
  250.   { Get current directory and save it for later }
  251.   GetDir( 0 , TheOldString );
  252.   { Check for need to add trailing \ }
  253.   WorkingDir := TheOldString;
  254.   WorkingDir := ScrollBox1.TheFWB.ForceTrailingBackSlash( WorkingDIr );
  255.   { Set up to do loop for selected files }
  256.   ThePosition := 1;
  257.   finished := false;
  258.   while not finished do
  259.   begin
  260.     { Call generic file getting routine based on current view}
  261.     case CurrentView of
  262.       1 : CurrentName := ScrollBox1.GetNextSelection( WorkingDir ,
  263.            ThePosition );
  264.       2 : CurrentName := FileListBox1.GetNextSelection( WorkingDir ,
  265.            ThePosition );
  266.     end;
  267.     { if returns blank string out of selections }
  268.     if CurrentName = '' then finished := true else
  269.     begin
  270.       { If its a directory signal error }
  271.       if (( FileGetAttr( CurrentName ) and faDirectory ) = faDirectory ) then
  272.       begin
  273.         if MessageDlg( 'Delete Directory ' + CurrentName + '?' , mtConfirmation,
  274.          mbYesNoCancel , 0 ) = mrYes then
  275.         begin
  276.           ScrollBox1.TheFWB.RecursivelyDeleteDirectory( CurrentName );
  277.           Scrollbox1.TheFWB.RemoveDirectory( Currentname );
  278.         end;
  279.       end
  280.       else
  281.       begin
  282.         { Otherwise get confirmation message and if OKed delete file }
  283.         if MessageDlg( 'Delete file ' + CurrentName + '?', mtConfirmation ,
  284.          mbYesNoCancel , 0 ) = mrYes then
  285.         begin
  286.           ScrollBox1.TheFWB.DeleteTheFile( Currentname );
  287.         end;
  288.       end;
  289.     end;
  290.   end;
  291.   if CurrentView = 1 then { Reset icon display if in view }
  292.    ScrollBox1.Update else FileListBox1.Update; { Otherwise update the FLB }
  293. end;
  294.  
  295. { Delphi wrote this; use it to rename selected files }
  296. procedure TCCFileMgrForm.BitBtn4Click(Sender: TObject);
  297. var ThePosition  : Integer;       { Loop counter   }
  298.     CurrentName ,                 { Holds work name}
  299.     TheOldString : String;        { Holds Dir      }
  300.     finished     : Boolean;       { Loop control   }
  301.     WorkingDir   : String;        { Holds mod wd   }
  302. begin
  303.   { Get current directory for later }
  304.   GetDir( 0 , TheOldString );
  305.   { Check for need to add trailing \ }
  306.   WorkingDir := TheOldString;
  307.   WorkingDir := ScrollBox1.TheFWB.ForceTrailingBackSlash( WorkingDIr );
  308.   { Set up to do loop for selected files }
  309.   ThePosition := 1;
  310.   finished := false;
  311.   while not finished do
  312.   begin
  313.     { Call generic file getting routine based on current view}
  314.     case CurrentView of
  315.       1 : CurrentName := ScrollBox1.GetNextSelection( WorkingDir ,
  316.            ThePosition );
  317.       2 : CurrentName := FileListBox1.GetNextSelection( WorkingDir ,
  318.            ThePosition );
  319.     end;
  320.     { If returns blank string then out of selections }
  321.     if CurrentName = '' then finished := true else
  322.     begin
  323.       { Otherwise set up the opendialog with filename and caption }
  324.       OpenDialog1.Filename := ExtractFilename( CurrentName );
  325.       { Use this to prevent error from changing directories }
  326.       OpenDialog1.Options := [ofNoChangeDir];
  327.       OpenDialog1.Title := 'Rename File';
  328.       { If not cancelled then do rename or signal error }
  329.       if OpenDialog1.Execute then
  330.       begin
  331.         ScrollBox1.TheFWB.RenameTheFile( CurrentName , OpenDialog1.Filename );
  332.       end;
  333.     end;
  334.   end;
  335.   if CurrentView = 1 then { Reset icon display if in view }
  336.    ScrollBox1.Update else FileListBox1.Update; { Otherwise update the FLB }
  337. end;
  338.  
  339. { Delphi wrote this; use it to make a new directory under current one }
  340. procedure TCCFileMgrForm.BitBtn5Click(Sender: TObject);
  341. begin
  342.   { Set up the dialog to get the new directory name }
  343.   OpenDialog2.FileName := '*.*';
  344.   OpenDialog2.Title := 'Enter Directory Name';
  345.   if OpenDialog1.Execute then
  346.   begin
  347.     { If not cancelled make new directory }
  348.     Scrollbox1.TheFWB.CreateNewDirectory( OpenDialog1.Filename );
  349.   end;
  350.   if CurrentView = 1 then { Reset icon display if in view }
  351.    ScrollBox1.Update else FileListBox1.Update; { Otherwise update the FLB }
  352. end;
  353.  
  354. { Delphi wrote this; use it to display and set data from options dialog }
  355. { Delphi wrote this; use it to synchronize the display with the listboxes }
  356. procedure TCCFileMgrForm.DirectoryListBox1Change(Sender: TObject);
  357. begin
  358.   { Set the display string to the uppercase of the current directory }
  359.   Label1.Caption := UpperCase( DirectoryListBox1.Directory );
  360. end;
  361.  
  362. { Delphi wrote this; use it to handle refreshing the scrollbox }
  363. procedure TCCFileMgrForm.FormPaint(Sender: TObject);
  364. var CurrentDirectory : String; { Get current dir }
  365. begin
  366.   { If updated, do refresh }
  367.   if ScrollBox1.IconsNeedRefreshing then
  368.   begin
  369.     Scrollbox1.Free;
  370.     ScrollBox1 := TFileIconPanelScrollBox.Create( Panel1 );
  371.     ScrollBox1.Parent := Panel1;
  372.     ScrollBox1.align := alClient;
  373.     ScrollBox1.Left := 0;
  374.     ScrollBox1.Top := 0;
  375.     ScrollBox1.Width := Panel1.Width - 32;
  376.     ScrollBox1.Height := Panel1.Height - 32;
  377.     ScrollBox1.IconsNeedRefreshing := false;
  378.     ScrollBox1.TheStoredHandle := Self.Handle;
  379.     GetDir( 0 , CurrentDirectory );
  380.     { Force trailing backslash }
  381.     CurrentDirectory := ScrollBox1.TheFWB.ForceTrailingBackSlash(
  382.      CurrentDirectory );
  383.     { Set the label to the new directory }
  384.     Label1.Caption := UpperCase( CurrentDirectory );
  385.     { Do the update }
  386.     ScrollBox1.GetIconsForEntireDirectory( CurrentDirectory );
  387.   end;
  388. end;
  389.  
  390. { Delphi wrote this; use it to respond to dbl-clicking FLB filename }
  391. procedure TCCFileMgrForm.FileListBox1DblClick(Sender: TObject);
  392. begin
  393.   { Call shellexec as a wrapper around ShellExecute API call }
  394.   { False indicates failure, signal error                    }
  395.   if not ShellExec( ExpandFileName( FileListbox1.Items[
  396.    FileListBox1.ItemIndex ] ), '' , '', false , SW_SHOWNORMAL , false ) then
  397.     MessageDlg('Could not Shell out to ' + FileListBox1.Items[
  398.      FileListBox1.ItemIndex ] , mtError, [mbOK], 0);
  399. end;
  400.  
  401. { Delphi wrote this; use it to reset the current view Icons/List }
  402. procedure TCCFileMgrForm.BitBtn8Click(Sender: TObject);
  403. var TheOldString : String; { Use to get current directory }
  404. begin
  405.   { Reset currentview }
  406.   if CurrentView = 1 then CurrentView := 2 else
  407.    CurrentView := 1;
  408.   { either show the windows boxes or the scrollbox }
  409.   case CurrentView of
  410.     1 : begin
  411.           { Make the listboxes invisible by changing align and width }
  412.           FileListBox1.Visible := false;
  413.           FileListBox1.Align := alNone;
  414.           FileListBox1.Width := 0;
  415.           DirectoryListBox1.Visible := false;
  416.           DirectoryListBox1.align := alNone;
  417.           DirectoryListBox1.Width := 0;
  418.           { Keep them from getting mouse clicks }
  419.           FileListBox1.Enabled := false;
  420.           DirectoryListBox1.Enabled := false;
  421.           { Have the scrollbox get mouse and be seen  by resetting align/wid}
  422.           Scrollbox1.align := alClient;
  423.           Scrollbox1.width := Panel1.Width - 32;
  424.           ScrollBox1.Enabled := true;
  425.           { Tell the scrollbox to repaint itself just in case }
  426.           ScrollBox1.Update;
  427.         end;
  428.     2 : begin
  429.           { Make the listboxes visible by resetting align and width}
  430.           FileListBox1.Visible := true;
  431.           DirectoryListBox1.Visible := true;
  432.           DirectoryListBox1.width := 145;
  433.           DirectoryListBox1.align := alLeft;
  434.           FileListBox1.align := alClient;
  435.           FileListBox1.Width := Panel1.Width - 32 - DirectoryListBox1.Width;
  436.           { Have them getting mouse clicks }
  437.           FileListBox1.Enabled := true;
  438.           DirectoryListBox1.Enabled := true;
  439.           { Have the scrollbox not get mouse and not be seen via align/width}
  440.           ScrollBox1.Enabled := false;
  441.           ScrollBox1.Visible := false;
  442.           ScrollBox1.align := alNone;
  443.           ScrollBox1.Width := 0;
  444.         end;
  445.   end;
  446.   Invalidate;
  447. end;
  448.  
  449. { Delphi wrote this; use it to do a file search display }
  450. procedure TCCFileMgrForm.BitBtn9Click(Sender: TObject);
  451. var CurrentDirectory : String; { Holds current directory }
  452. begin
  453.   { Set up and run the search dialog }
  454.   OpenDialog2.FileName := '*.*';
  455.   { if not cancelled do the search }
  456.   if OpenDialog2.Execute then
  457.   begin
  458.     { Get current directory }
  459.     GetDir( 0 , CurrentDirectory );
  460.     { Force trailing backslash }
  461.     CurrentDirectory :=
  462.      ScrollBox1.TheFWB.ForceTrailingBackSlash( CurrentDirectory );
  463.     { display recursive search results }
  464.     Scrollbox1.DisplayRecursiveSearchResults(
  465.      CurrentDirectory + OpenDialog2.Filename );
  466.     Label1.Caption := 'Search Results for ' + CurrentDirectory +
  467.      OpenDialog2.FileName;
  468.   end;
  469. end;
  470.  
  471. end.
  472.